﻿using System.Xml;
using System.Collections;


namespace Report.GetReportSQL
{
    public class SchemaXmlHelper
    {
        public static Hashtable getXSDfileSQL(string xsdfileName)
        {
            Hashtable ht = new Hashtable();
            
            XmlDocument xmlDoc = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略文档里面的注释
            XmlReader reader = XmlReader.Create(xsdfileName, settings);
            try
            {
                xmlDoc.Load(reader);
                XmlNode root = xmlDoc.GetElementsByTagName("Tables")[0];
                //XmlNodeList AdapterList  = xmlDoc.get("Tables");

                XmlNodeList AdapterList = root.ChildNodes;
                foreach (XmlNode AdapterRoot in AdapterList)
                {
                    //XmlNode TableNameNode = AdapterRoot;
                    //string TableName = AdapterRoot.Attributes["Name"].Value;
                    //string CommandText = AdapterRoot.InnerText;
                    ht.Add(AdapterRoot.Attributes["Name"].Value.ToString(), AdapterRoot.InnerText.ToString());
                }


            }
            finally
            {
                //释放资源
                reader.Close();
            }

            return ht;
        }

        public static Hashtable getJRXMLfileSQL(string xsdfileName)
        {
            string sTableName = string.Empty;
            string sQueryString = string.Empty;
            Hashtable ht = new Hashtable();

            XmlDocument xmlDoc = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略文档里面的注释
            XmlReader reader = XmlReader.Create(xsdfileName, settings);
            try
            {
                xmlDoc.Load(reader);
                XmlNode root = xmlDoc.GetElementsByTagName("jasperReport")[0];
                if (root.Attributes["name"] != null)
                    sTableName = root.Attributes["name"].Value.ToString();
                else
                    sTableName = "-- NA --";
                if (root["queryString"] != null)
                    sQueryString = root["queryString"].InnerText.ToString();
                else
                    sQueryString = "-- NA --";
                ht.Add(sTableName, sQueryString);

                XmlNodeList SubDataSetList = xmlDoc.GetElementsByTagName("subDataset");
                foreach (XmlNode aNode in SubDataSetList)
                {
                    if (aNode.Attributes["name"] != null)
                        sTableName = aNode.Attributes["name"].Value.ToString();
                    else
                        sTableName = "-- NA --";
                    if (aNode["queryString"] != null)
                        sQueryString = aNode["queryString"].InnerText.ToString();
                    else
                        sQueryString = "-- NA --";
                    ht.Add(sTableName, sQueryString);
                }


            }
            finally
            {
                //释放资源
                reader.Close();
            }

            return ht;
        }
    }
}